﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace YXShop.TemplateAction
{
    /// <summary>
    /// 标签中的语句块
    /// </summary>
    public partial class LabelParse
    {

        public enum EnumLabelType
        {
            SystemSetup, HeadOrBottomInfo, ProductClass, ProductBrand, ProductList, ReferChangehope, ProductListMakeup, ProductContent, Comment, CommentReplyInfo, OtherSetup, CreateURL, ShoppingVehicle, LeaveWrod, FreeLabel, Member, Search, OtherProduct, MemberLogin, FriedLink, ShoppingCar, ShoppingInfo, SendGroupBuyInfo, AricleList, ArticleContent, History, StoreList, StoreInfo, StoreType, StoreSearch, ScanType, ProductType, ProductFitting, ShoppingCarData
        };
        #region 标签参数
        /// <summary>
        /// Loop次数，Loop次数为0或者unLoop标签，为0
        /// </summary>
        protected int Param_Loop;
        /// <summary>
        /// 是否循环
        /// </summary>
        protected bool WhetherLoop;
        /// <summary>
        /// 标签格式是否有效
        /// </summary>
        protected bool FormatValid = true;
        /// <summary>
        /// 格式非法的说明
        /// </summary>
        protected string InvalidInfo = string.Empty;
        #endregion
        /// <summary>
        /// 标签原始内容
        /// </summary>
        protected string Label_Content = string.Empty;
        /// <summary>
        /// 标签的主体部份
        /// </summary>
        protected string Label_Primary = string.Empty;
        /// <summary>
        /// 标签中间插入代码部份
        /// </summary>
        protected string Label_Inserted = string.Empty;
        /// <summary>
        /// 标签类型
        /// </summary>
        protected EnumLabelType Param_LabelType;
        /// <summary>
        /// 标签所有参数
        /// </summary>
        protected LabelParameter[] _LblParams = null;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="labelcontent">标签的内容</param>
        public LabelParse(string labelcontent)
        {
            Label_Content = labelcontent;
        }

        /// <summary>
        /// 解析标签内容
        /// </summary>
        public void ParseContent()
        {
            int pos1 = Label_Content.IndexOf(']');
            int pos2 = Label_Content.LastIndexOf('[');
            int tempi = Label_Content.IndexOf("[");
            if (Label_Content.Length > 0 && pos1 > 1 && pos2 > 1)
            {
                Label_Primary = Label_Content.Substring(tempi + 1, pos1 - 1);
                int n = pos2 - pos1 - 1;
                if (n > 0)
                    Label_Inserted = Label_Content.Substring(pos1 + 1, n);
            }
            //wjl-->
            ParsePrimary();
        }
        /// <summary>
        /// 分析标签参数，拆分为数组
        /// </summary>
        private void ParsePrimary()
        {
            if (Label_Primary.IndexOf(",") > 0)
            {
                string[] _mass_p = Label_Primary.Split(',');
                if (_mass_p[0].Equals("YX:Loop"))
                {
                    Param_Loop = 1;
                    WhetherLoop = true;
                }
                else if (_mass_p[0].Equals("YX:unLoop"))
                {
                    Param_Loop = 0;
                    WhetherLoop = false;
                }
                else
                {
                    FormatValid = false;
                    InvalidInfo = "标签内容不是以[YX:unLoop或[YX:Loop开始";
                }
                int n = _mass_p.Length;
                IList<LabelParameter> l = new List<LabelParameter>();
                l.Clear();
                for (int i = 1; i < n; i++)
                {
                    if (!FormatValid) break;
                    string s = _mass_p[i];
                    int pos = s.IndexOf('=');
                    if (pos < 0)
                        continue;
                    LabelParameter p;
                    p.LPName = s.Substring(0, pos).Trim();
                    p.LPValue = s.Substring(pos + 1).Trim();
                    #region 对标签的必要参数进行一些处理
                    switch (p.LPName)
                    {
                        case "YX:LableType":
                            try
                            {
                                Param_LabelType = (EnumLabelType)System.Enum.Parse(typeof(EnumLabelType), p.LPValue);
                            }
                            catch
                            {
                                FormatValid = false;
                                InvalidInfo = "FS:LabelType指定的类型不存在";
                            }
                            break;
                        default:
                            AddParameter(p, ref l);
                            break;
                    }
                    #endregion 对标签的必要参数进行一些处理
                }
                int ln = l.Count;
                if (FormatValid && ln > 0)
                {
                    _LblParams = new LabelParameter[ln];
                    l.CopyTo(_LblParams, 0);
                }
            }
        }
        /// <summary>
        /// 解析标签生成HTML
        /// </summary>
        /// <returns></returns>
        public string Parse()
        {
            //if (!FormatValid)
            //    return Label_Content;
            switch (this.Param_LabelType)
            {
                case EnumLabelType.SystemSetup: return this.SystemParameter();
                case EnumLabelType.HeadOrBottomInfo: return this.HeadOrBottomInfo();
                case EnumLabelType.ProductList: return this.ProductList();
                case EnumLabelType.ProductFitting: return this.ProductFitting();
                case EnumLabelType.ProductListMakeup: return this.ProductListMakeup();
                case EnumLabelType.OtherSetup: return this.Other();
                case EnumLabelType.CreateURL: return this.CreateURL();
                case EnumLabelType.ProductClass: return this.ProductClass();
                case EnumLabelType.ProductContent: return this.ProductContent();
                case EnumLabelType.OtherProduct: return this.OtherProduct();
                case EnumLabelType.ProductBrand: return this.ProductBrand();
                case EnumLabelType.Comment: return this.Comment();
                case EnumLabelType.CommentReplyInfo: return this.CommentReplyInfo();
                case EnumLabelType.LeaveWrod: return this.LeaveWrod();
                case EnumLabelType.FreeLabel: return this.FreeLabel();
                case EnumLabelType.Member: return this.Member();
                case EnumLabelType.Search: return this.Search();
                case EnumLabelType.MemberLogin: return this.MemberLogin();
                case EnumLabelType.FriedLink: return this.FriedLink();
                case EnumLabelType.ShoppingCar: return this.ShoppingCar();
                case EnumLabelType.ShoppingCarData: return this.ShoppingCarInfo();
                case EnumLabelType.ShoppingInfo: return this.UserShoppingInfo();
                case EnumLabelType.SendGroupBuyInfo: return this.SendGroupBuyInfo();

                case EnumLabelType.AricleList: return this.AricleList();
                case EnumLabelType.ArticleContent: return this.ArticleContent();

                case EnumLabelType.ScanType: return this.ScanInfo();
                case EnumLabelType.History: return this.HistorySkim();

                    //商铺
                case EnumLabelType.StoreList: return this.StoreList();
                case EnumLabelType.StoreInfo: return this.StoreInfo();
                case EnumLabelType.StoreType: return this.StoreType();
                case EnumLabelType.StoreSearch: return this.StoreSearch();
                case EnumLabelType.ProductType: return this.ProductType();

                default:
                    return string.Empty;
            }
        }





        /// <summary>
        /// 将一个参数加入参数队列
        /// </summary>
        /// <param name="lp">标签参数</param>
        /// <param name="list">列表</param>
        private void AddParameter(LabelParameter lp, ref IList<LabelParameter> list)
        {
            bool flag = true;
            foreach (LabelParameter p in list)
            {
                if (p.LPName.Equals(lp.LPName))
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                list.Add(lp);
            }
        }
        /// <summary>
        /// 查找某一标签参数的值
        /// </summary>
        /// <param name="ParamName">标签参数的名称</param>
        /// <returns></returns>
        public string GetParamValue(string ParamName)
        {
            string result = string.Empty;
            if (_LblParams == null)
                return null;
            int n = _LblParams.Length;
            for (int i = 0; i < n; i++)
            {
                LabelParameter p = _LblParams[i];
                if (p.LPName.Equals(ParamName))
                {
                    result = p.LPValue;
                    break;
                }
            }
            return result.Equals(string.Empty) ? null : result;
        }
        /// <summary>
        /// 标签的初始内容
        /// </summary>
        public string Content
        {
            get { return Label_Content; }
        }
    }
}
